package jp.ac.kobe_u.cs.cream;

import jp.ac.kobe_u.cs.cream.*;
import java.util.*;

public class NotEquals extends Constraint {
    private Variable v[];

    public NotEquals(Network net, Variable v0, Variable v1) {
	this(net, new Variable[] {v0, v1});
    }

    public NotEquals(Network net, Variable v[]) {
	super(net);
	this.v = (Variable[])v.clone();
    }

    protected Constraint copy(Network net) {
	return new NotEquals(net, Constraint.copy(v, net));
    }

    protected boolean isModified() {
	return isModified(v);
    }

    protected boolean satisfy(Trail trail) {
	for (int i = 0; i < v.length; i++) {
	    Domain d = v[i].getDomain();
	    if (d.size() != 1)
		continue;
	    Object elem = d.element();
	    for (int j = 0; j < v.length; j++) {
		if (i == j)
		    continue;
		Domain d1 = v[j].getDomain().delete(elem);
		if (d1.isEmpty())
		    return false;
		v[j].updateDomain(d1, trail);
	    }
	}
	return true;
    }

    public String toString() {
	return "NotEquals(" + Constraint.toString(v) + ")";
    }
}
